package com.quan.mall.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.core.bean.R;
import com.quan.commons.core.biz.support.MyBaseController;
import com.quan.commons.core.utils.*;
import com.quan.mall.commons.vo.PmsBrandVo;
import com.quan.system.commons.vo.SysUserVo;
import com.quan.commons.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.quan.mall.commons.vo.PmsCategoryBrandRelationVo;
import com.quan.mall.entity.PmsCategoryBrandRelation;
import com.quan.mall.service.PmsCategoryBrandRelationService;


/**
 * 商品-品牌分类关联
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2021-02-09 11:44:02
 */
@Slf4j
@Api(tags = "商品-品牌分类关联")
@RestController
@RequestMapping("/mall/pmscategorybrandrelation")
public class PmsCategoryBrandRelationController extends MyBaseController {

    @Autowired
    private PmsCategoryBrandRelationService service;

    /**
     * 保存
     * @param vo
     * @return
     */
    @SysLog("保存")
    @PostMapping("save")
    public R save(@Valid @RequestBody PmsCategoryBrandRelationVo vo) {
        this.service.saveDetail(vo);
        return R.ok();
    }

    /**
     * 修改
     * @param vo
     * @return
     */
    @SysLog("修改")
    @PostMapping("update")
    public R update(@Valid @RequestBody PmsCategoryBrandRelationVo vo) {
        this.service.updateById(vo);
        return R.ok();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @SysLog("删除")
    @PostMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        if (null != ids && ids.length > 0) {
            this.service.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 多条件查询信息详情
     * @param vo
     * @return
     */
    @SysLog("查询详情")
    @GetMapping("/info")
    public R info(PmsCategoryBrandRelationVo vo) {
        QueryWrapper<PmsCategoryBrandRelation> queryWrapper = new QueryWrapper<PmsCategoryBrandRelation>(vo);
        PmsCategoryBrandRelation pmsCategoryBrandRelation = this.service.getOne(queryWrapper);
        return R.ok().put("data", pmsCategoryBrandRelation);
    }

    /**
     * 根据ID查询信息详情
     * @param {id}
     * @return
     */
    @SysLog("查询详情")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PmsCategoryBrandRelation pmsCategoryBrandRelation = this.service.getById(id);
        return R.ok().put("data", pmsCategoryBrandRelation);
    }

    /**
     * 列表查询
     * @param vo
     * @return
     */
    @SysLog("查询列表")
    @GetMapping(value = "list")
    public Object list(PmsCategoryBrandRelationVo vo) {
        List<PmsCategoryBrandRelationVo> list = this.service.queryList(new PageUtils(request), vo);
        return R.ok().put("data", list);
    }

    /**
     * 分页查询
     * @param vo
     * @return
     */
    @SysLog("分页查询")
    @GetMapping(value = "page")
    public R queryPage(PmsCategoryBrandRelationVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().put("data", page);
    }

    /**
     * 获取当前品牌关联的所有分类列表列表
     */
    @GetMapping(value = "/category/list")
    public R catelogList(@RequestParam Map<String, Object> params, @RequestParam("brandId") Long brandId){
        List<PmsCategoryBrandRelation> data = service.
                list(new QueryWrapper<PmsCategoryBrandRelation>().eq("brand_id",brandId));

        return R.ok().put("data", data);
    }

    /**
     * 根据分类ID获取品牌
     * @param cateId
     * @return
     */
    @GetMapping(value = "/brands/list")
    public R relationBransList(@RequestParam(value = "cateId",required = true) Long cateId) {

        List<PmsBrandVo> vos = service.getBrandsByCatId(cateId);

        List<PmsBrandVo> collect = vos.stream().map(item -> {
            PmsBrandVo brandVo = new PmsBrandVo();
            brandVo.setBrandId(item.getId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());

        return R.ok().put("data",collect);
    }

}
